package com.cloudsigns.factorymanager2.produce.activity.produce_team;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartDailyFeedbackBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderBean;
import com.cloudsigns.factorymanager2.produce.iview.produce_team.IProduceUnusualReplyDailyFeedbackView;
import com.cloudsigns.factorymanager2.produce.presenter.produce_team.ProduceUnusualReplyDailyFeedbackPresenter;
import com.cloudsigns.factorymanager2.produce.utils.ProduceForTeamEvent;
import com.cloudsigns.factorymanager2.project.customview.GeneralPurposePoupWindow;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import de.greenrobot.event.EventBus;

public class ProduceTeamFeedbackReplyForTeamActivity extends BaseActivity implements ItemClickListener, IProduceUnusualReplyDailyFeedbackView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_unusual_type, tv_unusual_advice, tv_unusual_reason, tv_feedback_date, tv_reply_content, tv_confirm_person, tv_confirm_time;
    private EditText edt_reply_content;
    private RelativeLayout rl_replay_content;
    private ImageView img_reply_content;
    private View v_2,v_3,v_4;
    private LinearLayout ll_reply_content,ll_confirm_people,ll_confirm_time;
    private Button btn_submit;

    private GeneralPurposePoupWindow generalPurposePoupWindow;
    private String datas[] = {"制作完成", "修补完成", "购买完成", "重新发货已经到达", "施工协调已经到位", "文件调整已经清晰"};

    private WorkPartDailyFeedbackBean workPartDailyFeedbackBean;
    private ProduceUnusualReplyDailyFeedbackPresenter produceUnusualReplyDailyFeedbackPresenter;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_feedback_reply_for_team);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_unusual_type = findViewById(R.id.tv_unusual_type);
        tv_unusual_advice = findViewById(R.id.tv_unusual_advice);
        tv_unusual_reason = findViewById(R.id.tv_unusual_reason);
        tv_feedback_date = findViewById(R.id.tv_feedback_date);
        edt_reply_content = findViewById(R.id.edt_reply_content);
        tv_reply_content = findViewById(R.id.tv_reply_content);
        tv_confirm_person = findViewById(R.id.tv_confirm_person);
        tv_confirm_time = findViewById(R.id.tv_confirm_time);
        ll_reply_content=findViewById(R.id.ll_reply_content);
        rl_replay_content = findViewById(R.id.rl_replay_content);
        img_reply_content = findViewById(R.id.img_reply_content);
        v_2 = findViewById(R.id.v_2);
        v_3=findViewById(R.id.v_3);
        v_4=findViewById(R.id.v_4);
        ll_confirm_people=findViewById(R.id.ll_confirm_people);
        ll_confirm_time=findViewById(R.id.ll_confirm_time);
        btn_submit = findViewById(R.id.btn_submit);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.abnormal_return));

        Intent intent = getIntent();
        workPartDailyFeedbackBean = (WorkPartDailyFeedbackBean) intent.getSerializableExtra(ConstantUtil.KEY_1);

        CommonEnum.WorkPartSubType subType = workPartDailyFeedbackBean.getSubType();
        if (null != subType) {
            if (subType == CommonEnum.WorkPartSubType.PLAN_UNUSUAL) {
                tv_unusual_type.setText(getResources().getString(R.string.plan_unusual));
            } else if (subType == CommonEnum.WorkPartSubType.PRODUCE_UNUSUAL) {
                tv_unusual_type.setText(getResources().getString(R.string.machin_unusual));
            }
        }

        tv_unusual_reason.setText(workPartDailyFeedbackBean.getUnusualInfo());
        tv_unusual_advice.setText(workPartDailyFeedbackBean.getUnusualAdvice());
        tv_feedback_date.setText(DateUtil.getYyyy_Mm_Dd(workPartDailyFeedbackBean.getFeedbackDate()));

        CommonEnum.FeedbackStatus unusualStatus = workPartDailyFeedbackBean.getFeedbackStatus();
        if (null != unusualStatus) {
            if (unusualStatus == CommonEnum.FeedbackStatus.CONFIRM_FINISH) {
                rl_replay_content.setVisibility(View.VISIBLE);

                v_3.setVisibility(View.GONE);
                v_4.setVisibility(View.GONE);
                ll_reply_content.setVisibility(View.GONE);
                ll_confirm_people.setVisibility(View.GONE);
                ll_confirm_time.setVisibility(View.GONE);

                btn_submit.setVisibility(View.VISIBLE);

            } else if (unusualStatus == CommonEnum.FeedbackStatus.WAIT_REPLY_CONFIRM) {
                rl_replay_content.setVisibility(View.GONE);

                v_3.setVisibility(View.GONE);
                v_4.setVisibility(View.GONE);
                ll_reply_content.setVisibility(View.VISIBLE);
                ll_confirm_people.setVisibility(View.GONE);
                ll_confirm_time.setVisibility(View.GONE);

                btn_submit.setVisibility(View.GONE);

                tv_reply_content.setText(workPartDailyFeedbackBean.getCompleteUnusualDesc());

            } else if (unusualStatus == CommonEnum.FeedbackStatus.REPLY_CONFIRM_FINISH) {
                rl_replay_content.setVisibility(View.GONE);

                v_3.setVisibility(View.VISIBLE);
                v_4.setVisibility(View.VISIBLE);
                ll_confirm_people.setVisibility(View.VISIBLE);
                ll_confirm_time.setVisibility(View.VISIBLE);

                btn_submit.setVisibility(View.GONE);

                tv_reply_content.setText(workPartDailyFeedbackBean.getCompleteUnusualDesc());
                tv_confirm_person.setText(workPartDailyFeedbackBean.getConfirmCompleteUnusualAccountName() + ":" + workPartDailyFeedbackBean.getConfirmCompleteUnusualAccountId());
                tv_confirm_time.setText(DateUtil.getYyyy_Mm_Dd(workPartDailyFeedbackBean.getConfirmCompleteUnusualDate()));
            }
        }
        produceUnusualReplyDailyFeedbackPresenter = new ProduceUnusualReplyDailyFeedbackPresenter(this);
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        img_reply_content.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
    }

    private void showReplyContentWindow(View anchor) {
        generalPurposePoupWindow = new GeneralPurposePoupWindow(this, datas);
        generalPurposePoupWindow.setItemClick(this);
        generalPurposePoupWindow.showPopupWindow(anchor);
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 回复内容
            case R.id.img_reply_content:
                hideSoftInputView();
                showReplyContentWindow(v_2);
                break;
            // 提交
            case R.id.btn_submit:
                if (TextUtils.isEmpty(edt_reply_content.getText().toString())){
                    showToast(getResources().getString(R.string.reply_content_no_empty));
                    return;
                }else {
                    produceUnusualReplyDailyFeedbackPresenter.produceUnusualReplyDailyFeedback();
                }
                break;
        }
    }

    @Override
    public void itemClick(View view, int position) {
        edt_reply_content.setText(datas[position]);
    }

    @Override
    public String getCompleteUnusualDesc() {
        return edt_reply_content.getText().toString();
    }

    @Override
    public String getOrderId() {
        return workPartDailyFeedbackBean.getOrderId();
    }

    @Override
    public String getProductId() {
        return workPartDailyFeedbackBean.getProductId();
    }

    @Override
    public String getWorkPartFeedbackId() {
        return workPartDailyFeedbackBean.getWorkPartFeedbackId();
    }

    @Override
    public String getWorkPartId() {
        return workPartDailyFeedbackBean.getWorkPartId();
    }

    @Override
    public String getWorkPartOrderId() {
        return workPartDailyFeedbackBean.getWorkPartOrderId();
    }

    @Override
    public void getProduceUnusualReplyDailyFeedbackSuccessed() {
        showToast(getResources().getString(R.string.reply_success));
        EventBus.getDefault().post(new ProduceForTeamEvent());
        finish();
    }

    @Override
    public void getProduceUnusualReplyDailyFeedbackFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
